package stylistcrm.core.services.security.dto;

import com.buybuyyi.stylistcrm.Entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import stylistcrm.core.services.security.exception.NavigationClosedLoopException;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanwc on 2016/9/7.
 */
public class NavigationDto extends Entity<Long>{


    private NavigationDto parent ;

    private List<NavigationDto> children;

    public List<NavigationDto> getChildren() {
        return children;
    }

    public void addChild(NavigationDto nav) throws NavigationClosedLoopException {
        if(children == null){
            children = new ArrayList<>();
        }
        //闭环检测
        NavigationDto temp = parent;
        while(temp != null){
            if(temp.getId() == nav.getId()){
                throw new NavigationClosedLoopException(temp);
            }
            temp= temp.getParent();
        }
        children.add(nav);
    }


    /**
     * 导航名称
     */
    private String name;

    /**
     * 导航编码
     */
    private String code;


    /**
     * 导航备注
     */

    private String remark;

    /**
     * 排序
     */
    private BigDecimal sort;


    /**
     * 子导航集合
     */
    private List<NavigationDto> subNavigation;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<NavigationDto> getSubNavigation() {
        return subNavigation;
    }

    public void add(NavigationDto navigation){
        if(navigation == null)return;
        if(this.subNavigation == null){
            this.subNavigation = new ArrayList<>();
        }
        this.subNavigation.add(navigation);
    }

    public NavigationDto getParent() {
        return parent;
    }

    public BigDecimal getSort() {
        return sort;
    }

    public void setSort(BigDecimal sort) {
        this.sort = sort;
    }

    public void setParent(NavigationDto parent) {
        this.parent = parent;
    }
}
